package com.wedata.海康云;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.wedata.HttpUtils;

import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class 人员管理 extends BaseService{

    static String url_info = "https://open.hikyun.com/artemis/api/eits/fdcs/v1/person/page";
    static String url_add = "https://open.hikyun.com/artemis/api/eits/fdcs/v1/person/add";
    static String url_upload = "https://open.hikyun.com/artemis/api/eits/fdcs/v1/person/face/upload";
    String url_update = "https://open.hikyun.com/artemis/api/eits/fdcs/v1/person/update";
    String url_personGroup = "https://open.hikyun.com/artemis/api/eits/fdcs/v1/personGroup/page";
    String url_add_personGroup = "https://open.hikyun.com/artemis/api/eits/fdcs/v1/personGroup/add";
    String url_delete = "https://open.hikyun.com/artemis/api/eits/fdcs/v1/person/delete";

    public static void main(String[] args) {
        //addPerson();
        queryPersonPage();
        //updatePerson();
        //queryPersonList();
    }

    /**
     * 查询人员信息--分页
     * @return
     */
    public static String queryPersonPage() {
        try{
            FileWriter fileWriter = new FileWriter("E:\\海康云人脸数据.txt");
            JSONObject param = new JSONObject();
            param.set("projectId",projectId);
            param.set("productCode",productCode);
            //param.set("status",1);//状态，0：删除，1是有效,默认1
            Integer total = 10492;//11141;
            param.set("pageSize",11);

            //param.set("personGroupId",2165211671547568L);//安装人员上传的照片
            //{"msg":"","code":"200","data":{"pageSize":10,"list":[{"orgPathId":"/361013479217840","addressInfo":{"detailInfo":"","areaInfo":""},"certType":111,"cardNums":[],"orgName":"默认组织","orgPath":"默认组织","sex":1,"remark":"","personNum":"2164323921940144","orgId":"361013479217840","personName":"工程测试1","isBackList":0,"faceUrl":"http://93d01acb-yunyao-isapi.oss-cn-hangzhou.aliyuncs.com/photo/2094041613979312.jpg?Expires=1962931361&OSSAccessKeyId=LTAI5t87fYBKrYRHkmXAEyZ9&Signature=Kff6wDVbJOQBz8abQcesyMH9s48%3D","productCode":"1646638166955231","jobPost":"","phone":"","extAcsAuth":{"userVerifyMode":"","isDelayOpen":0,"openCode":"","accessAuthority":1},"personId":"2164323921940144","certNum":"","personType":"normal","projectId":"361013479217840","personGroups":[{"groupId":"2165211671547568","groupName":"工程测试"}],"status":1}],"total":1,"pageNo":1}}
            //param.set("personGroupId",1127973407079944L);
            //{"msg":"","code":"200","data":{"pageSize":10,"list":[{"orgPathId":"/361013479217840/2164837673736880","addressInfo":{"detailInfo":"","areaInfo":""},"cardNums":[],"orgName":"WEGYMER健萌","orgPath":"默认组织/WEGYMER健萌","personNum":"5408","orgId":"2164837673736880","personName":"李祥","isBackList":0,"faceUrl":"http://93d01acb-yunyao-isapi.oss-cn-hangzhou.aliyuncs.com/photo/2165978737028784.jpg?Expires=1962951699&OSSAccessKeyId=LTAI5t87fYBKrYRHkmXAEyZ9&Signature=K1s3pUFIKeUSDm4c1tMpS%2BMRHvQ%3D","productCode":"1646638166955231","phone":"","extAcsAuth":{"userVerifyMode":null,"isDelayOpen":0,"openCode":null,"accessAuthority":1},"personId":"2094084777455280","personType":"normal","projectId":"361013479217840","personGroups":[{"groupId":"1127973407079944","groupName":"超级权限"},{"groupId":"2165426758269616","groupName":"兴旺总部"},{"groupId":"2094718015330992","groupName":"归心苑芒果私教馆"}],"personLabels":[],"status":1},{"orgPathId":"/361013479217840/2164837673736880","certType":111,"orgPath":"默认组织/WEGYMER健萌","remark":"","orgId":"2164837673736880","extAcsAuth":{"userVerifyMode":"","isDelayOpen":0,"openCode":"","accessAuthority":1},"certNum":"","personType":"normal","personGroups":[{"groupId":"1127973407079944","groupName":"超级权限"},{"groupId":"2094718015330992","groupName":"归心苑芒果私教馆"}],"personLabels":[{"personLabelName":"员工","id":"1126995538341960"}],"addressInfo":{"detailInfo":"","areaInfo":""},"cardNums":[],"orgName":"WEGYMER健萌","sex":0,"personNum":"362","plateNum":"","personName":"邓雨诗","isBackList":0,"faceUrl":"https://piced.hikyun.com/downloadData/photo/925703119864688.jpg?KEY2=2595ec7200c&KEY1=e02fa96c445dd3185018306453e504d4&accessKey=17a848f2&v=1","productCode":"1646638166955231","jobPost":"","phone":"","personId":"2165522994286256","projectId":"361013479217840","status":1},{"orgPathId":"/361013479217840/2164837673736880","addressInfo":{"detailInfo":"","areaInfo":""},"cardNums":[],"orgName":"WEGYMER健萌","orgPath":"默认组织/WEGYMER健萌","sex":0,"personNum":"69","orgId":"2164837673736880","personName":"邓郴","isBackList":0,"faceUrl":"https://piced.hikyun.com/downloadData/photo/2093829571620528.jpg?KEY2=2595ec7200c&KEY1=12fa165e8b36a99e710c1bc1349dcd54&accessKey=17a848f2&v=1","productCode":"1646638166955231","phone":"","extAcsAuth":{"userVerifyMode":null,"isDelayOpen":0,"openCode":null,"accessAuthority":1},"personId":"2164772650077872","personType":"normal","projectId":"361013479217840","personGroups":[{"groupId":"681810807112","groupName":"五楼门禁"},{"groupId":"1127973407079944","groupName":"超级权限"},{"groupId":"2165426758269616","groupName":"兴旺总部"},{"groupId":"2094718015330992","groupName":"归心苑芒果私教馆"}],"personLabels":[{"personLabelName":"员工","id":"1126995538341960"}],"status":1}],"total":3,"pageNo":1}}
            //param.set("personIds",new String[]{"792996529064"});
            //{"msg":"","code":"200","data":{"pageSize":10,"list":[{"orgPathId":"/361013479217840/2164837673736880","addressInfo":{"detailInfo":"","areaInfo":""},"cardNums":[],"orgName":"WEGYMER健萌","orgPath":"默认组织/WEGYMER健萌","sex":1,"personNum":"yl1886736","orgId":"2164837673736880","personName":"杨林","isBackList":0,"productCode":"1646638166955231","extAcsAuth":{"userVerifyMode":null,"isDelayOpen":0,"openCode":null,"accessAuthority":1},"personId":"792996529064","personType":"normal","projectId":"361013479217840","personGroups":[{"groupId":"563131970044840","groupName":"约课调研组"}],"status":1}],"total":1,"pageNo":1}}
            for(int pageNo=1; pageNo<=1; pageNo++){
                param.set("pageNo",pageNo);
                JSONObject header = new JSONObject("{\"access_token\":\""+getToken()+"\"}");
                JSONObject jsonObject = HttpUtils.sendPost("https://open.hikyun.com/artemis/api/eits/fdcs/v1/person/page",header,param);
                JSONArray list = jsonObject.getJSONObject("data").getJSONArray("list");

                for(int i=0; i<list.size(); i++){
                    JSONObject object = list.getJSONObject(i);
            /*"orgPathId": "/361013479217840/2164837673736880",
                    "orgPath": "默认组织/WEGYMER健萌",
                    "personNum": "225696",
                    "orgId": "2164837673736880",
                    "personName": "季宇翔",
                    "isBackList": 0,
                    "faceUrl": "https://piced.hikyun.com/downloadData/photo/1127498232630184.jpg?KEY2=2597e1bf9f1&KEY1=f2be7a2c68c01b5acf720f3435dd0987&accessKey=17a848f2&v=1",
                    "productCode": "1646638166955231",
                    "personId": "696919526312",
                    "personType": "normal",
                    "projectId": "361013479217840",
                    "personGroups": [{
                "groupId": "2165489413053360",
                        "groupName": "湖南路私教馆"
            }],
            "status": 1*/
                    StringBuffer sb = new StringBuffer();
                    sb.append(object.getStr("orgPathId")).append("\t");
                    sb.append(object.getStr("orgPath")).append("	");
                    sb.append(object.getStr("personNum")).append("	");
                    sb.append(object.getStr("orgId")).append("	");
                    sb.append(object.getStr("personName")).append("	");
                    sb.append(object.getStr("isBackList")).append("	");
                    sb.append(object.getStr("faceUrl")).append("	");
                    sb.append(object.getStr("personId")).append("	");
                    sb.append(object.getStr("personType")).append("	");
                    sb.append(object.getStr("personGroups")).append("	");
                    sb.append("\n");
                    fileWriter.write(sb.toString());
                    fileWriter.flush();
                }
            }
            fileWriter.close();
        }catch (Exception ex){
            System.out.println(ex);
        }

        return null;
    }
    //全量查询--数据太多，会超时
    public static String queryPersonList(){
       // String[] personIds = new String[]{"1217801547048","1127082285519144","1745878780456","564994031155240","1126621515948488","565042118366792","564796734805576","564953568220744","563029464815176","1426302408264","1285407348296","1916642373000","2164323921940144"};
        //String[] personIds = new String[]{"1916642373000","2164323921940144","231477933808"};
        JSONObject param = new JSONObject();
        param.set("projectId",projectId);
        param.set("productCode",productCode);
        //param.put("personIds", personIds);
        param.put("personNum", "237750");
        JSONObject header = new JSONObject("{\"access_token\":\""+getToken()+"\"}");
        JSONObject jsonObject = HttpUtils.sendPost("https://open.hikyun.com/artemis/api/eits/v1/person/queryPerson",header,param);
        System.out.println(jsonObject);
        JSONArray jsonArray = jsonObject.getJSONArray("data");
        System.out.println(jsonArray);
        for(int i=0; i<jsonArray.size(); i++){
            System.out.println(jsonArray.getJSONObject(i));
            System.out.println(jsonArray.getJSONObject(i).getStr("personId"));
        }
        return null;
    }

    /**
     * 添加人员
     * @return
     */
    public static String addPerson() {
        JSONObject param = new JSONObject();
        param.set("productCode", productCode);
        param.set("projectId", projectId);

        param.set("personName", "黎增辉");
        param.set("sex", 2);//性别,1:男，2：女
        param.set("personNum", "61243");
        param.set("orgId", orgId);
        //param.set("personGroupIds", new String[]{"563131970044840"});
        /*JSONObject face = new JSONObject();
        face.set("faceId","2095357352598448");
        param.set("face", face);*/
        //{"msg":"","code":"200","data":{"personId":"792996529064"}}
        //{"msg":"人员编号已存在","code":"0x14e2110C","data":null} -- 重复提交

        JSONObject header = new JSONObject("{\"access_token\":\""+getToken()+"\"}");
        JSONObject jsonObject = HttpUtils.sendPost(url_add, header, param);
        System.out.println(jsonObject);
        return null;
    }
    /**
     * 批量添加人员
     * @return
     */
    public static String addPersons() {
        JSONObject param = new JSONObject();
        param.set("productCode", productCode);
        param.set("projectId", projectId);
        List persons = new ArrayList<>();
        JSONObject person = new JSONObject();
        person.set("","");
        persons.add(person);
        param.set("persons", persons);
        JSONObject header = new JSONObject("{\"access_token\":\""+getToken()+"\"}");
        JSONObject jsonObject = HttpUtils.sendPost("https://open.hikyun.com/artemis/api/eits/v1/person/batchAdd", header, param);
        System.out.println(jsonObject);
        return null;
    }

    /**
     * 修改人员
     * @return
     */
    public static String updatePerson() {
        JSONObject param = new JSONObject();
        param.set("productCode", productCode);
        param.set("projectId", projectId);

        param.set("personId", "792996529064");
        //param.set("sex", 1);//性别,1:男，2：女
        //param.set("personNum", "yl1886736");// 不设置是会默认=personId
        //param.set("orgId", orgId);
        param.set("personGroupIds", new String[]{"563131970044840"});
        param.set("isBackList",0);
        //{"msg":"","code":"200","data":{"personId":"792996529064"}}
        //{"msg":"人员编号已存在","code":"0x14e2110C","data":null} -- 重复提交

        JSONObject header = new JSONObject("{\"access_token\":\""+getToken()+"\"}");
        JSONObject jsonObject = HttpUtils.sendPost("https://open.hikyun.com/artemis/api/eits/fdcs/v1/person/update", header, param);
        System.out.println(jsonObject);
        return null;
    }

}
